package com.tan.utils;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author 谭永超 [www.tanyongchao.tk:8090/SumDome/index]
 * @Date 2018/8/20 14:57
 * @Description
 */
@Component
@Order(1)
@Aspect
public class AopLogPrint {
    public static final Log log = LogFactory.getLog(AopLogPrint.class);
    @Pointcut("execution(public * com.tan.controller.selectController.*(..))")
    public void log(){
    }
    @Before("log()")
    public void doBefore(JoinPoint jinPoint){
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        // url
        log.info("url="+request.getRequestURL());
        // method
        log.info("method="+request.getMethod());
        // ip
        log.info("ip="+request.getRemoteAddr());
        // 类方法
        //log.info("class_method="+joinPoint.getSignature().getDeclaringType()+"."+joinPoint.getSignature().getDeclaringTypeName());
        //类方法
        log.info("class_method"+jinPoint.getSignature().getDeclaringType()+"."+jinPoint.getSignature().getDeclaringTypeName());
        //参数
        log.info("args="+jinPoint.getArgs());
    }
    // after 在方法执行之后执行
    @After("log()")
    public void doAfter(){
    }
    @AfterReturning(returning = "object",pointcut = "log()")
    public void doAfterReturning(Object object){
    }
}
